ARD2  RC2
Airbag Reference Demonstrator using MPC5604P
ME.c
Go to the documentation of this file.
00001 
00019 #include "derivative.h"
00020 #include "Compile_Options.h"
00021 #include "ME.h"
00022 /*
00023  ******************************************************************************
00024  * Constants
00025  ******************************************************************************
00026  */
00028 static const uint32_t cu32EnabledModes = 
00029   (STOP0_DISABLED | HALT0_DISABLED | RUN3_ENABLED | RUN2_ENABLED | \
00030    RUN1_ENABLED   | TEST_DISABLED);
00032 static const uint32_t cau32RunModeConfig[] = 
00033 {
00034   /* RUN 0 - External Clock (40 MHz on demo board) */
00035   (PDO_OFF  | MVRON_ON  | DATA_FLASH_ON  | CODE_FLASH_ON  | PLL1_OFF | \
00036    PLL0_OFF | XOSC0_ON  | IRC_16MHZ_OFF  | SYSCLK_IS_XOSC), 
00037   
00038   /* RUN 1 - Internal Clock */
00039   (PDO_OFF  | MVRON_ON  | DATA_FLASH_ON  | CODE_FLASH_ON  | PLL1_OFF | \
00040    PLL0_OFF | XOSC0_OFF | IRC_16MHZ_ON   | SYSCLK_IS_IRC), 
00041   
00042   /* RUN 2 - XOSC without IRC on for CLKOUT */
00043   (PDO_OFF  | MVRON_ON  | DATA_FLASH_ON  | CODE_FLASH_ON | PLL1_OFF | \
00044    PLL0_ON  | XOSC0_ON  | IRC_16MHZ_ON   | SYSCLK_IS_XOSC), 
00045   
00046   /* RUN 3 - PLL with PLL1 on for CLKOUT */
00047   (PDO_OFF  | MVRON_ON  | DATA_FLASH_ON  | CODE_FLASH_ON | PLL1_ON  | \
00048    PLL0_ON  | XOSC0_ON  | IRC_16MHZ_OFF   | SYSCLK_IS_PLL0), 
00049 };
00051 static const uint32_t cu32Halt0ModeConfig = 
00052   /* HALT 0 */
00053   (PDO_OFF  | MVRON_OFF | DATA_FLASH_LP  | CODE_FLASH_LP  | PLL1_OFF | \
00054    PLL0_OFF | XOSC0_OFF | IRC_16MHZ_ON   | SYSCLK_IS_IRC);
00055 /* Contains STOP mode configuration */
00056 static const uint32_t cu32Stop0ModeConfig = 
00057   /* STOP 0 */
00058   (PDO_OFF  | MVRON_OFF | DATA_FLASH_OFF | CODE_FLASH_OFF | PLL1_OFF | \
00059    PLL0_OFF | XOSC0_OFF | IRC_16MHZ_OFF   | SYSCLK_IS_OFF);
00060 /*
00061  ******************************************************************************
00062  * u8fnInitDefaultPeripheralPresets
00063  ******************************************************************************
00064  */
00065 uint8_t u8fnInitDefaultPeripheralPresets(void)
00066 {
00067   /* Declare local variables */
00068   uint8_t u8Status;
00069   
00070   /* Init Local variables */
00071   u8Status =  CLEAR;
00072   
00073   /* Verify that we are not in the middle of a transition before configuring */
00074   if((CLEAR == ME_TRANSITION) && (TRUE == ME_REGULATOR_READY))
00075   {
00076     /* Load default configuration where peripherals are always on */
00077     ME.RUNPC[PERIPH_ALWAYS_ON].B.RUN3 = TRUE;
00078     ME.RUNPC[PERIPH_ALWAYS_ON].B.RUN2 = TRUE;
00079     ME.RUNPC[PERIPH_ALWAYS_ON].B.RUN1 = TRUE;
00080     ME.RUNPC[PERIPH_ALWAYS_ON].B.RUN0 = TRUE;
00081     ME.RUNPC[PERIPH_ALWAYS_ON].B.DRUN = TRUE;
00082     ME.RUNPC[PERIPH_ALWAYS_ON].B.SAFE = CLEAR;
00083     ME.RUNPC[PERIPH_ALWAYS_ON].B.TEST = CLEAR;
00084     
00085     ME.LPPC[PERIPH_ALWAYS_ON].B.STANDBY0 = TRUE;
00086     ME.LPPC[PERIPH_ALWAYS_ON].B.STOP0    = TRUE;
00087     ME.LPPC[PERIPH_ALWAYS_ON].B.HALT0    = TRUE;
00088     
00089     /* Load default configuration where peripherals are always off */
00090     ME.RUNPC[PERIPH_ALWAYS_OFF].B.RUN3 = CLEAR;
00091     ME.RUNPC[PERIPH_ALWAYS_OFF].B.RUN2 = CLEAR;
00092     ME.RUNPC[PERIPH_ALWAYS_OFF].B.RUN1 = CLEAR;
00093     ME.RUNPC[PERIPH_ALWAYS_OFF].B.RUN0 = CLEAR;
00094     ME.RUNPC[PERIPH_ALWAYS_OFF].B.DRUN = CLEAR;
00095     ME.RUNPC[PERIPH_ALWAYS_OFF].B.SAFE = CLEAR;
00096     ME.RUNPC[PERIPH_ALWAYS_OFF].B.TEST = CLEAR;
00097     
00098     ME.LPPC[PERIPH_ALWAYS_ON].B.STANDBY0 = CLEAR;
00099     ME.LPPC[PERIPH_ALWAYS_ON].B.STOP0    = CLEAR;
00100     ME.LPPC[PERIPH_ALWAYS_ON].B.HALT0    = CLEAR;
00101   }
00102   else
00103   {
00104     /* Flag as an error */
00105     u8Status = ME_HW_NOT_READY;
00106   }
00107   
00108   return(u8Status);
00109 }
00110 /*
00111  ******************************************************************************
00112  * u8fnConfigPeripheralForMCUModePreset
00113  ******************************************************************************
00114  */
00115 uint8_t u8fnConfigPeripheralForMCUModePreset(const uint8_t u8PeripheralIndex, \
00116                                              const uint8_t u8ConfigurationIndex)
00117 {
00118   /* Declare local variables */
00119   uint8_t u8Status;
00120   
00121   /* Init local variables */
00122   u8Status = CLEAR;
00123   
00124   /* Verify that the passed values are within the possibilities of HW */
00125   if((MAX_PERIPH_INDEX_POSSIBLE > u8PeripheralIndex) && \
00126      (MAX_PERIPH_CONFIG_POSSIBLE > u8ConfigurationIndex))
00127   {
00128     /* If so, set the peripheric to the interrupt */
00129     ME.PCTL[u8PeripheralIndex].R = u8ConfigurationIndex;
00130   }
00131   else
00132   {
00133     /* Else, flag as error */
00134     u8Status = ME_INVALID_INDEX;
00135   }
00136   
00137   return(u8Status);
00138 }
00139 /*
00140  ******************************************************************************
00141  * u8fnCurrentOperationMode
00142  ******************************************************************************
00143  */
00144 uint8_t u8fnCurrentOperationMode(void)
00145 {
00146   return((uint8_t)ME.GS.B.S_CURRENTMODE);
00147 }
00148 /*
00149  ******************************************************************************
00150  * u8fnInitDefaultOpModes
00151  ******************************************************************************
00152  */
00153 uint8_t u8fnInitDefaultOpModes(void)
00154 {
00155   /* Declare local variables */
00156   uint8_t u8Status;
00157   uint8_t u8Counter;
00158   
00159   /* Init local variables */
00160   u8Status =  CLEAR;
00161   u8Counter = CLEAR;
00162   
00163   /* Verify that we are not in the middle of a transition before configuring */
00164   /* Verify that we are in RUND since we can only configure in this mode     */
00165   if((CLEAR == ME_TRANSITION) && (TRUE == ME_REGULATOR_READY))
00166   {
00167     if(DRUN_MODE == u8fnCurrentOperationMode())
00168     {
00169       for (u8Counter = (sizeof(cau32RunModeConfig) / BYTES_IN_32); \
00170            u8Counter > CLEAR; u8Counter--)
00171       {
00172         ME.RUN[u8Counter - 1u].R = cau32RunModeConfig[u8Counter - 1u];
00173       }
00174     
00175       ME.HALT0.R = cu32Halt0ModeConfig;
00176       ME.STOP0.R = cu32Stop0ModeConfig;
00177       
00178       /* Enable modes as stipulated through the constant variable */
00179       ME.MER.R = cu32EnabledModes;
00180     }
00181     else
00182     {
00183       /* Mode was not DRUN - we cannot change behaviors */
00184       u8Status = ME_INVALID_RUN_MODE;
00185     }
00186   }
00187   else
00188   {
00189     /* Flag error */
00190     u8Status = ME_HW_NOT_READY;
00191   }
00192   
00193   return(u8Status);
00194 }
00195 /*
00196  ******************************************************************************
00197  * u8fnInitDefaultOpModes
00198  ******************************************************************************
00199  */
00200 uint8_t u8fnSwitchOpModes(const uint8_t u8Mode)
00201 {
00202   /* Declare local variables */
00203   uint8_t u8Status;
00204   uint16_t u16Counter;
00205   
00206   u16Counter = CLEAR;
00207   u8Status   = ME_OP_MODE_CHANGE_DIDNT_OCCUR;
00208   
00209   /* Switching modes is performed by a two-step operation */
00210   /* 1) ME_MCTL must be written with a special key and the desired mode */
00211   ME.MCTL.R = (uint32_t)((u8Mode << 28u) | CHANGE_MODE_KEY_1);
00212   /* 2) ME_MCTL must be written with a second key and the desired mode */
00213   ME.MCTL.R = (uint32_t)((u8Mode << 28u) | CHANGE_MODE_KEY_2);
00214   
00215   /* Wait for the operation to take */
00216   do
00217   {
00218     /* If hardware states that we are done with the transition */
00219     if(CLEAR == ME.GS.B.S_MTRANS)
00220     {
00221       /* Verify that we have changed operation mode to what we have asked */
00222       if(u8Mode == u8fnCurrentOperationMode())
00223       {
00224         u8Status = CLEAR;
00225       }
00226       else
00227       {
00228         /* Nothing */
00229       }
00230     }
00231     else
00232     {
00233       /* loop */
00234     }
00235   }while((u16Counter++ < SWITCH_MODE_TIMEOUT) & 
00236          (u8Status == ME_OP_MODE_CHANGE_DIDNT_OCCUR));
00237 
00238   return(u8Status);
00239 }
00240 /*
00241  ******************************************************************************
00242  *
00243  *  End of file.
00244  *
00245  ******************************************************************************
00246  */